package v1

import (
	"strings"
	"yuanxin-admin/internal/common/ioc"
	"yuanxin-admin/internal/common/session"
	"yuanxin-admin/internal/dto"
	"yuanxin-admin/internal/service"

	"gitee.com/botaoshow/yuanxin/web/response"
	"gitee.com/botaoshow/yuanxin/web/y"
	"github.com/gin-gonic/gin"
)

type SysUserController struct {
	sysUserService service.ISysUserService
}

func NewSysUserController() *SysUserController {
	return &SysUserController{
		sysUserService: ioc.Inject[service.ISysUserService]((service.ISysUserService)(nil)),
	}
}

func (s *SysUserController) ImageCaptcha(c *gin.Context) {
	key, img, err := s.sysUserService.ImageCaptcha(c)
	if err != nil {
		response.Fail(c, err.Error())
	} else {
		response.Success(c, y.Map{
			"captchaKey":   key,
			"captchaImage": img,
		})
	}
}

func (s *SysUserController) Login(c *gin.Context) {
	var loginDto dto.SysUserLoginDto
	if err := c.ShouldBindBodyWithJSON(&loginDto); err != nil {
		response.Fail(c, err.Error())
		return
	}
	if token, err := s.sysUserService.Login(c, loginDto); err != nil {
		response.Fail(c, err.Error())
	} else {
		response.Success(c, token)
	}
}
func (s *SysUserController) Logout(c *gin.Context) {
	token := c.GetHeader("Authorization")
	//是不是以 Bearer 开头
	if strings.HasPrefix(token, "Bearer ") {
		token = token[7:]
	}
	if err := s.sysUserService.Logout(c, token); err != nil {
		response.Fail(c, err.Error())
	} else {
		response.SuccessDefault(c)
	}
}

func (s *SysUserController) ChangePassword(c *gin.Context) {
	var d struct {
		OldPassword string `json:"oldPassword"`
		NewPassword string `json:"newPassword"`
	}
	if err := c.ShouldBindBodyWithJSON(&d); err != nil {
		response.Fail(c, err.Error())
		return
	}
	id := session.GetUserId(c)
	if flag, err := s.sysUserService.
		ChangePassword(c, id, d.OldPassword, d.NewPassword); err != nil {
		response.Fail(c, err.Error())
	} else {
		response.ToAjax(c, flag)
	}
}

func (s *SysUserController) UpdateStatus(c *gin.Context) {
	if flag, err := s.sysUserService.
		UpdateStatus(c, c.Param("id"), c.Param("status")); err != nil {
		response.Fail(c, err.Error())
	} else {
		response.ToAjax(c, flag)
	}
}
func (s *SysUserController) QueryList(c *gin.Context) {
	var d dto.SysUserQueryDTO
	if err := c.ShouldBindQuery(&d); err != nil {
		response.Fail(c, err.Error())
		return
	}
	if list, err := s.sysUserService.QueryList(c, &d); err != nil {
		response.Fail(c, err.Error())
	} else {
		response.Success(c, list)
	}
}
func (s *SysUserController) Save(c *gin.Context) {
	var d dto.SysUserDTO
	if err := c.ShouldBindBodyWithJSON(&d); err != nil {
		response.Fail(c, err.Error())
		return
	}
	if flag, err := s.sysUserService.Save(c, &d); err != nil {
		response.Fail(c, err.Error())
	} else {
		response.ToAjax(c, flag)
	}
}

func (s *SysUserController) Update(c *gin.Context) {
	var d dto.SysUserDTO
	if err := c.ShouldBindBodyWithJSON(&d); err != nil {
		response.Fail(c, err.Error())
		return
	}
	if flag, err := s.sysUserService.Update(c, c.Param("id"), &d); err != nil {
		response.Fail(c, err.Error())
	} else {
		response.ToAjax(c, flag)
	}
}

func (s *SysUserController) Delete(c *gin.Context) {
	if flag, err := s.sysUserService.Delete(c, c.Param("id")); err != nil {
		response.Fail(c, err.Error())
	} else {
		response.ToAjax(c, flag)
	}
}
